package com.lemon.smartsafetynew.ui.adapter.work;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.ApplicationInfo;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.entity.event.main.MainPanelButtonEvent;
import com.lemon.smartsafety.entity.main.MainPanelButton;
import com.lemon.smartsafety.entity.main.MainRcvItemBase;
import com.lemon.smartsafety.ui.adapter.common.RcvViewType;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafetynew.entity.User;
import com.lemon.smartsafetynew.entity.event.work.WorkUIEvent;
import com.lemon.smartsafetynew.util.user.AccountUtil;

import java.util.List;

import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2017/7/3.
 * [工作]页面主菜单布局适配器文件
 */

public class WorkOptionItemViewRec implements AdapterItem<MainRcvItemBase<List<MainPanelButton>>> {

    private LoggerHelper loggerHelper = new LoggerHelper(WorkOptionItemViewRec.class);

    private RecyclerView mRecyclerView;

    private WorkOptionItemAdapter mAdapter;

    private int mainPanelColumnNumber = Constants.MAIN_PANEL_ICON_COUNT;

    @Override
    public int getLayoutResId() {
        return R.layout.view_recycler_new;
    }

    @Override
    public void bindViews(View root) {

        mRecyclerView = (RecyclerView) root.findViewById(R.id.recyclerView);

//        User user = AccountUtil.getUser();
//        if(user != null) {
//            if(ApplicationInfo.WORKER_CODE.equals(user.getUserType())) {
//                mainPanelColumnNumber = 2;
//            } else if(ApplicationInfo.SAFER_CODE.equals(user.getUserType())) {
//                mainPanelColumnNumber = 4;
//            }
//        }

    }

    @Override
    public void setViews() {

    }

    @Override
    public void handleData(MainRcvItemBase<List<MainPanelButton>> mainRcvItemBase, int position) {

        if (RcvViewType.MainRcvViewType.TYPE_MAIN_OPTION != mainRcvItemBase.getViewType()) {
            return;
        }

        final List<MainPanelButton> items = mainRcvItemBase.getData();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mRecyclerView.getContext(), mainPanelColumnNumber);
        mAdapter = new WorkOptionItemAdapter(items);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }


    public class WorkOptionItemAdapter extends LemonRcvAdapter<MainPanelButton> {


        public WorkOptionItemAdapter(@Nullable List<MainPanelButton> data) {
            super(data);
        }


        @Override
        public int getItemCount() {
            return getData().size();
        }

        @NonNull
        @Override
        public AdapterItem createItem(Object type) {
            return new MainOptionItemRecItem();
        }

        public class MainOptionItemRecItem implements AdapterItem<MainPanelButton> {

            LinearLayout ripple;
            ImageView iconImage;
            TextView textView;

            private int mPosition;
            private MainPanelButton mainPanelButton;

            @Override
            public int getLayoutResId() {
                return R.layout.item_main_panel_new;
            }

            @Override
            public void bindViews(View root) {
                ripple = (LinearLayout) root.findViewById(R.id.ripple);
                iconImage = (ImageView) root.findViewById(R.id.label_icon);
                textView = (TextView) root.findViewById(R.id.label_name);
            }


            @Override
            public void setViews() {
                ripple.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        WorkUIEvent event = new WorkUIEvent(mainPanelButton);
                        AppService.getBus().post(event);
                    }
                });

            }

            @Override
            public void handleData(MainPanelButton data, int position) {
                mainPanelButton = data;
                mPosition = position;
                iconImage.setImageResource(data.getIconRes());
                textView.setText(data.getTitleRes());

            }
        }
    }

}